﻿#ifndef DLGMODBUSRTU_H
#define DLGMODBUSRTU_H

#include <QDialog>
#include <QSettings>
#include <QPixmap>
#include <QButtonGroup>

#include "communication/serialport.h"
#include "communication/crc16.h"

namespace Ui {
class DlgModbusRtu;
}

class DlgModbusRtu : public QDialog
{
    Q_OBJECT

public:
    explicit DlgModbusRtu(QSettings* settings, SerialPort* serialPort, QWidget *parent = nullptr);
    ~DlgModbusRtu() override;

protected:
    void closeEvent(QCloseEvent *event) override;

private:
    void initUI();
    void init();
    void readSingleRegister(unsigned short address, unsigned short num);
    void writeSingleRegister(unsigned short address, unsigned short value);
    void writeMultiRegister(unsigned short address, unsigned short num, const QByteArray& valueArray);


private:
    Ui::DlgModbusRtu *ui;

    // 串口打开/关闭指示灯
    QPixmap m_serialPortOpened;
    QPixmap m_serialPortClosed;

    // 读取/写入
    QButtonGroup* m_bgReadWrite;

private:
    // INI配置文件
    QSettings* m_settings;

    // Modbus485 RTU串口
    SerialPort* m_serialPort;

    // 串口打开标识
    bool m_opened;
    // 串口读取等待时间
    int m_readWaitInterval;

private slots:
    void onReadWriteChanged(int id);

    void onOpenSerialPort();
    void onSaveRegister();
    void onHardwareVersion();
    void onSoftwareVersion();
    void onStatusRegister();
    void onRealtimePosition();
    void onRealtimeSpeed();
    void onRealtimeCurrent();
    void onSubdivision32();
    void onMotorDirection();
    void onMotorEnable();
    void onZero();
    void onTwoZero();
    void onZeroRun();
    void onRunSpeed();
    void onRunStop();
    void onPointMove();
    void onRunTime();
    void onRunImpulse();
    void onRunAbsolutePosition();
    void onSetAbsolutePosition();
    void onReadWarning();
    void onClearWarning();

    void onSend();
};

#endif // DLGMODBUSRTU_H
